package com.sskj.aikeeper.hangqing.activity;

import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sskj.aikeeper.R;
import com.sskj.aikeeper.component.DaggerUserDataComponent;
import com.sskj.aikeeper.presenter.NicknamePresenter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.HideUtil;
import com.sskj.munite.MinuteView;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/24
 */
@Route(path = RConfig.APP_ACTIVITY_NICKNAME)
public class NicknameActivity extends BaseActivity<NicknamePresenter> {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.et_nickname)
    EditText etNickname;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_nickname;
    }

    @Override
    public NicknamePresenter getPresenter() {
        return new NicknamePresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                etNickname.setText(userData.getNickname());
                if (isEmpty(userData.getNickname())) {
                    tvTitle.setText(App.INSTANCE.getString(R.string.app_nicknameActivity1));
                    btnConfirm.setText(App.INSTANCE.getString(R.string.app_nicknameActivity2));
                } else {
                    tvTitle.setText(App.INSTANCE.getString(R.string.app_nicknameActivity3));
                    btnConfirm.setText(App.INSTANCE.getString(R.string.app_nicknameActivity4));
                }
            } else {
                userData = null;
            }
        });

    }

    @Override
    public void initData() {
        ClickUtil.click(300, btnConfirm, () -> {
            if (isEmpty(etNickname)) {
                toastHint(etNickname);
                return;
            }
            mPresenter.setNichName(getText(etNickname));
        });

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, NicknameActivity.class);
        context.startActivity(intent);
    }

    public void setNicknameOK(String nickname) {
        userData.setNickname(nickname);
        userViewModel.update(userData);
        finish();
    }
}
